﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Business.WebsiteBusinessCategory;
using BCData.Common.Business.WebsiteBusinessIntroduction;
using BCDto.Common.Business.WebsiteBusinessCategory;
using BCDto.Common.Business.WebsiteBusinessIntroduction;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.Business.WebsiteBusinessIntroduction;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.Business.WebsiteBusinessIntroduction
{
    public class WebsiteBusinessIntroductionService : IWebsiteBusinessIntroductionService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteBusinessIntroductionData websiteBusinessIntroductionData;
        private readonly IWebsiteBusinessCategoryData websiteBusinessCategoryData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteBusinessIntroductionEntity);

        public WebsiteBusinessIntroductionService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteBusinessIntroductionData websiteBusinessIntroductionData,
            IWebsiteBusinessCategoryData websiteBusinessCategoryData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteBusinessIntroductionData = websiteBusinessIntroductionData;
            this.websiteBusinessCategoryData = websiteBusinessCategoryData;
        }

        #region 检查官网业务简介是否存在
        public bool Exists(int websiteBusinessIntroductionId)
        {
            return this.websiteBusinessIntroductionData.Exists(websiteBusinessIntroductionId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网业务简介
        public WebsiteBusinessIntroductionDto Add(WebsiteBusinessIntroductionRequestDto requestDto,AdminDto adminDto)
        {
            if (!this.websiteBusinessCategoryData.Exists(requestDto.WebsiteBusinessCategoryId).Result)
            {
                throw new ArgumentException("此业务分类不存在!");
            }
            var categoryEntity = this.websiteBusinessCategoryData.GetEntity(requestDto.WebsiteBusinessCategoryId).Result;
            var entity = requestDto.As<WebsiteBusinessIntroductionEntity>();
            entity.Title = categoryEntity.Title;
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            return this.websiteBusinessIntroductionData.Add(entity).GetAwaiter().GetResult().As<WebsiteBusinessIntroductionDto>();
        }
        #endregion

        #region 修改官网业务简介
        public WebsiteBusinessIntroductionDto Update(WebsiteBusinessIntroductionPutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteBusinessIntroductionId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (!this.websiteBusinessCategoryData.Exists(putDto.WebsiteBusinessCategoryId).Result)
            {
                throw new ArgumentException("此业务分类不存在!");
            }
            var categoryEntity = this.websiteBusinessCategoryData.GetEntity(putDto.WebsiteBusinessCategoryId).Result;
            var entity = putDto.As<WebsiteBusinessIntroductionEntity>();
            entity.Title = categoryEntity.Title;
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteBusinessIntroductionData.Update(entity).GetAwaiter().GetResult().As<WebsiteBusinessIntroductionDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteBusinessIntroductionId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网业务简介
        public bool Delete(int websiteBusinessIntroductionId)
        {
            if (!this.Exists(websiteBusinessIntroductionId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteBusinessIntroductionData.Delete(websiteBusinessIntroductionId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteBusinessIntroductionId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网业务简介
        public WebsiteBusinessIntroductionDto Get(int websiteBusinessIntroductionId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteBusinessIntroductionId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteBusinessIntroductionData.GetEntity(websiteBusinessIntroductionId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteBusinessIntroductionDto>();
        }
        #endregion

        #region 获取官网业务简介列表
        public IEnumerable<WebsiteBusinessIntroductionDto> GetList(int? businessCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, businessCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.websiteBusinessIntroductionData.GetList(businessCategoryId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteBusinessIntroductionDto>>();
        }
        #endregion

        #region 获取官网业务分类列表
        public IEnumerable<WebsiteBusinessCategoryViewDto> GetList(string tagcode)
        {
            string cacheKey = this.redisService.GetKey(key, tagcode);
            var categoryResult = this.redisService.TryGet(cacheKey, () =>
            {
                var result = this.websiteBusinessCategoryData.GetList(tagcode).Result;
                var viewDtos = result.As<IEnumerable<WebsiteBusinessCategoryViewDto>>();
                foreach (var item in viewDtos)
                {
                    item.Introductions = this.websiteBusinessIntroductionData.GetList(item.WebsiteBusinessCategoryId).Result.As<List<WebsiteBusinessIntroductionViewDto>>();
                }
                return viewDtos;
            }, TimeSpan.FromMinutes(30));

            return categoryResult;
        }
        #endregion

        #region 获取官网业务简介分页
        public PageableList<WebsiteBusinessIntroductionDto> Query(string tagcode, int? businessCategoryId, string itemName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, tagcode, businessCategoryId, itemName, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteBusinessIntroductionData.Query(tagcode, businessCategoryId, itemName, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteBusinessIntroductionDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteBusinessIntroductionDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 启用官网业务简介
        public bool Enabled(int websiteBusinessIntroductionId)
        {
            if (!this.Exists(websiteBusinessIntroductionId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteBusinessIntroductionData.Enabled(websiteBusinessIntroductionId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteBusinessIntroductionId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用官网业务简介
        public bool Disabled(int websiteBusinessIntroductionId)
        {
            if (!this.Exists(websiteBusinessIntroductionId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteBusinessIntroductionData.Disabled(websiteBusinessIntroductionId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteBusinessIntroductionId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 静态站点生成使用接口
        #region 获取业务介绍(带条数限制)
        public IEnumerable<WebsiteBusinessCategoryViewDto> GetWebsiteBusinessIntroductionStaticPagingList(string tagcode, int? size)
        {
            var result = this.websiteBusinessCategoryData.GetList(tagcode).Result;
            var list = result.As<List<WebsiteBusinessCategoryViewDto>>();
            List<WebsiteBusinessCategoryViewDto> viewDtos = new List<WebsiteBusinessCategoryViewDto>();
            if (size.HasValue)
            {
                viewDtos = list.Take(size.Value).ToList();
            }
            else
            {
                viewDtos = list;
            }
            foreach (var item in viewDtos)
            {
                item.Introductions = this.websiteBusinessIntroductionData.GetList(item.WebsiteBusinessCategoryId).Result.As<List<WebsiteBusinessIntroductionViewDto>>();
            }
            return viewDtos;
        }
        #endregion


        #endregion
    }
}